package com.itheima.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.store.Company;
import com.itheima.service.store.CompanyService;
import com.itheima.service.store.impl.CompanyServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * CompanyServlet
 * 2021/10/17  17:07
 * created by linjinyang
 */

//uri:/store/company?operation=list
@WebServlet(urlPatterns = "/store/company")
public class CompanyServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取参数
        String method = request.getParameter("operation");
        if ("list".equals(method)){
            this.list(request,response);
        }else if ("toAdd".equals(method)){
            this.toAdd(request,response);
        }else if ("save".equals(method)){
            try {
                this.save(request,response);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if ("toEdit".equals(method)){
            this.toEdit(request,response);
        }else if ("edit".equals(method)){
            this.edit(request,response);
        }else if ("delete".equals(method)){
            this.delete(request,response);
        }
    }


    /*
    list
     */
    private void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //数据以分页形式展示
        //将数据展示在页面上
        //调用service
        int page = 1;
        int size = 5;
        //判断非空
        if (StringUtils.isNotBlank(request.getParameter("page"))){
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))){
            size = Integer.parseInt(request.getParameter("size"));
        }
        //返回一个PageInfo对象
        PageInfo all = companyService.findAll(page, size);
        //将获取到的数据传入请求域
        request.setAttribute("page",all);
        //请求转发
        request.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(request,response);
    }

    /*
    toAdd
     */
    private void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        //请求转发至新建表单的页面
        request.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(request,response);
    }

    /*
    save
     */
    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        String name = request.getParameter("name");
        String licenseId = request.getParameter("licenseId");
        String city = request.getParameter("city");
        String address = request.getParameter("address");
        String representative = request.getParameter("representative");
        String phone = request.getParameter("phone");
        String companySize = request.getParameter("companySize");
        String industry = request.getParameter("industry");
        String state = request.getParameter("state");
        String expirationDate1 = request.getParameter("expirationDate");
        String remarks = request.getParameter("remarks");

        Company company = new Company();
        company.setName(name);
        company.setLicenseId(licenseId);
        company.setCity(city);
        company.setAddress(address);
        company.setRepresentative(representative);
        company.setPhone(phone);
        company.setCompanySize(companySize);
        company.setIndustry(industry);
        company.setState(Integer.parseInt(state));
        company.setRemarks(remarks);
        //判断是否获取到时间，并传入对象
        if (expirationDate1 == "" || expirationDate1 == null){
            //获取当前时间
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String now = sdf.format(date);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date expirationDate2 = df.parse(now);
            company.setExpirationDate(expirationDate2);
        }else {
            //设置日期格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date expirationDate = df.parse(expirationDate1);
            company.setExpirationDate(expirationDate);
        }

        //调用service层
        companyService.save(company);

        //重定向至展示页面
        response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
    }

    /*
    toEdit(加载页面，单单只有这个是没数据的，所以要获取数据，加载到界面上)
     */
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String id = request.getParameter("id");
        //调用service层
        //CompanyService companyService = new CompanyServiceImpl();
        Company company = companyService.findById(id);
        //将数据传至请求域，供页面加载(这东西上传后，前端是在哪里接收的呢？)
        request.setAttribute("company",company);
        //请求转发
        request.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(request,response);
    }

    /*
    edit
     */
    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将填写的表单数据封装成对象,没有携带时间可以不转格式
        Company company = BeanUtil.fillBean(request, Company.class);
        //调用service层
        //CompanyService companyService = new CompanyServiceImpl();
        companyService.update(company);
        //重定向
        response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
    }

    /*
    delete
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取封装对象
        Company company = BeanUtil.fillBean(request, Company.class);
        //调用service
        //CompanyService companyService = new CompanyServiceImpl();
        companyService.delete(company);//id如何获取
        //重定向
        response.sendRedirect(request.getContextPath()+"/store/company?operation=list");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
